package com.ts.service.achievement.impl;

import com.ts.dto.MonographDTO;
import com.ts.dto.MemberDTO;
import com.ts.entity.Monograph;
import com.ts.entity.Member;
import com.ts.mapper.achievement.MonographMapper;
import com.ts.mapper.achievement.MonographMemberMapper;
import com.ts.service.achievement.MonographService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MonographServiceImpl implements MonographService {

    @Autowired
    private MonographMapper monographMapper;

    @Autowired
    private MonographMemberMapper monographMemberMapper;

    /**
     * 提交一个专著成果（包含成员信息）
     *
     * @param monographDTO 专著成果数据传输对象
     */
    @Override
    @Transactional
    public void save(MonographDTO monographDTO) {
        // 1. 保存专著基本信息
        Monograph monograph = new Monograph();
        BeanUtils.copyProperties(monographDTO, monograph);

        // 2. 设置系统字段
        LocalDateTime now = LocalDateTime.now();
        monograph.setCreateTime(now);
        monograph.setUpdateTime(now);
        monograph.setSubmitTime(now);
        monograph.setCreateBy(1L);
        monograph.setUpdateBy(1L);
        monograph.setSubmitUserId(1L);
        monograph.setAuditStatus(0); // 默认未审核状态

        // 3. 保存专著主表信息
        monographMapper.save(monograph);

        // 4. 处理成员信息（如果是团队成果）
        saveMembers(monographDTO.getMemberList(), monograph.getId());
    }
    /**
     * 保存成员信息（与示例完全一致的实现）
     * @param memberDTOs 成员DTO列表
     * @param projectId 专著ID
     */
    private void saveMembers(List<MemberDTO> memberDTOs, Long projectId) {
        if (memberDTOs != null && !memberDTOs.isEmpty()) {
            List<Member> members = memberDTOs.stream()
                    .map(memberDTO -> {
                        Member member = new Member();
                        BeanUtils.copyProperties(memberDTO, member);
                        member.setProjectId(projectId);
                        return member;
                    })
                    .collect(Collectors.toList());

            // 批量保存成员信息
            monographMemberMapper.batchInsert(members);
        }
    }
    @Override
    @Transactional
    public void update(MonographDTO monographDTO) {
        // 1. 更新专著基本信息
        Monograph monograph = new Monograph();
        BeanUtils.copyProperties(monographDTO, monograph);

        // 设置更新时间和更新人
        monograph.setUpdateTime(LocalDateTime.now());
        monograph.setUpdateBy(1L); // 实际项目中应该从登录用户获取

        monographMapper.updateById(monograph);

        // 2. 先删除原有成员，再添加新成员
        monographMemberMapper.deleteByProjectId(monographDTO.getId());
        saveMembers(monographDTO.getMemberList(), monographDTO.getId());
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 删除成员信息
        monographMemberMapper.deleteByProjectId(id);
        // 删除专著信息
        monographMapper.deleteById(id);
    }

    @Override
    public MonographDTO getById(Long id) {
        // 获取专著基本信息
        Monograph monograph = monographMapper.selectById(id);
        if (monograph == null) {
            return null;
        }

        MonographDTO dto = new MonographDTO();
        BeanUtils.copyProperties(monograph, dto);

        // 获取成员信息
        List<Member> members = monographMemberMapper.selectByProjectId(id);
        List<MemberDTO> memberDTOs = members.stream()
                .map(member -> {
                    MemberDTO memberDTO = new MemberDTO();
                    BeanUtils.copyProperties(member, memberDTO);
                    return memberDTO;
                })
                .collect(Collectors.toList());

        dto.setMemberList(memberDTOs);
        return dto;
    }

    @Override
    public List<MonographDTO> listAll() {
        List<Monograph> monographs = monographMapper.selectAll();
        return monographs.stream()
                .map(monograph -> {
                    MonographDTO dto = new MonographDTO();
                    BeanUtils.copyProperties(monograph, dto);
                    return dto;
                })
                .collect(Collectors.toList());
    }
}
